//
//  DoctorBasicViewController.m
//  CareLink
//
//  Created by apple on 16/8/19.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "DoctorBasicViewController.h"
#import "CLTimeViewController.h"
#import "doctMessageModel.h"
#import "DoctorSelectModel.h"
#import <UIKit/UIView.h>

//@property (nonatomic, copy) NSString *easymob_id;
@interface DoctorBasicViewController ()<UICollectionViewDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *flowerBtn;
@property (weak, nonatomic) IBOutlet UILabel *hospitalLabel;
@property (weak, nonatomic) IBOutlet UIButton *operationBtn;
@property (weak, nonatomic) IBOutlet UIButton *bannerBtn;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *doctorImageview;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
/**
 *  接诊条件按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *requireBtn;
/**
 *  医生简介按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *doctorBtn;
/**
 *  诊断时间按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *timeDiagnoseBtn;


@property (nonatomic,strong) NSArray *dutiesArr;
/**
 *  中间btn
 */
@property (weak, nonatomic) IBOutlet UIButton *middleBtn;
/**
 *  预约collectionview
 */
@property (weak, nonatomic) IBOutlet UICollectionView *dateCollectionView;
/**
 *  预约collectionview的layout样式
 */
@property (nonatomic,strong) UICollectionViewFlowLayout *dateLayout;
/**
 *  滚动条父视图
 */
@property (weak, nonatomic) IBOutlet UIView *scrpView;
/**
 *  接诊条件
 */
@property (nonatomic,strong) UIView *requirementView;
/**
 *  存储cell的字典
 */
@property (nonatomic,strong) NSMutableDictionary *cellDict;
/**
 *  滚动条
 */
@property (nonatomic,weak) UIView *twigView;
@end

@implementation DoctorBasicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 设置分割线
    [self setupSeparateLine];
//    // 请求网路数据
//    [self loadDocTimeData];
    // 设置dateCollectionView
    [self setDateCollectionV];
    /**
     *  信息
     */
    NSLog(@"%@",self.sickDataDict);
    // 设置接诊视图的背景颜色
    self.requirementView.backgroundColor =[UIColor whiteColor];
    // 设置医生信息栏
    [self setDocMessage];
    // 设置滚动条
    [self settwig];
    // 添加按钮点击事件
    [self clickBtn];
    // 方式一
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    // 设置navgatationcontroller的title
    self.navigationItem.title = @"基本信息";
}
// 按钮点击
- (void)clickBtn
{
    [self.requireBtn addTarget:self action:@selector(scrollTwig:) forControlEvents:UIControlEventTouchUpInside];
    [self.doctorBtn addTarget:self action:@selector(scrollTwig:) forControlEvents:UIControlEventTouchUpInside];
    [self.timeDiagnoseBtn addTarget:self action:@selector(scrollTwig:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 按钮点击，滚动条动
// 按钮点击改变滚动条的位置
- (void)scrollTwig:(UIButton *)sender
{
  [self.dateCollectionView setContentOffset:CGPointMake(sender.tag * self.scrpView.bounds.size.width, 0) animated:YES];
    
}
// 设置滚动条
- (void)settwig
{
    // 添加医生基本信息界面滚动条
    UIView *twigView = [[UIView alloc]init];
    twigView.backgroundColor = [UIColor colorWithHex:0x20c6c6];
    twigView.frame = CGRectMake(0, 0, self.scrpView.width, 2);
    [self.scrpView addSubview:twigView];
    self.twigView = twigView;
    // 约束
    [twigView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.requireBtn.mas_centerX);
        make.width.equalTo(self.requireBtn);
        make.height.offset(2);
        make.bottom.equalTo(self.scrpView);
    }];
}
// 设置dateCollectionView
- (void)setDateCollectionV
{
    // 数据源
    self.dateCollectionView.dataSource = self;
    self.dateCollectionView.delegate = self;
//    // 注册单元格
//    [self.dateCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:datecellid];
    // 隐藏滚动条
    self.dateCollectionView.showsVerticalScrollIndicator = NO;
    self.dateCollectionView.showsHorizontalScrollIndicator = NO;
    self.dateCollectionView.pagingEnabled = YES;
    self.dateCollectionView.bounces = NO;
    // 设置flowelayout
    UICollectionViewFlowLayout *dateLayout = (UICollectionViewFlowLayout *)self.dateCollectionView.collectionViewLayout;
    // 设置layout的样式
    dateLayout.minimumLineSpacing = 0;
    dateLayout.minimumInteritemSpacing = 0;
    dateLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 赋值给全局
    self.dateLayout = dateLayout;
}

// 设置医生信息栏
- (void)setDocMessage
{
    
    [self.doctorImageview sd_setImageWithURL:[NSURL URLWithString:self.selectModel.doctor_portrait]];
    self.nameLabel.text = self.selectModel.doctor_name;
    self.titleLabel.text = self.selectModel.doctor_title_name;
    self.hospitalLabel.text = self.selectModel.doctor_hospital_name;
    [self.flowerBtn setTitle:[NSString stringWithFormat:@"鲜花数:%zd",self.selectModel.flower] forState:UIControlStateNormal];
    [self.bannerBtn setTitle:[NSString stringWithFormat:@"锦旗数%zd",self.selectModel.banner] forState:UIControlStateNormal];
    [self.operationBtn setTitle:[NSString stringWithFormat:@"预约数%zd",self.selectModel.operation_count] forState:UIControlStateNormal];
}
// 设置分割线
- (void)setupSeparateLine
{
    //左边
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
    [self.middleBtn addSubview:imageview];
    // 约束
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(1);
        make.left.top.offset(0);
        make.bottom.offset(-2);
    }];
    // 右边
    UIImageView *imageview1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
    [self.middleBtn addSubview:imageview1];
    // 约束
    [imageview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(1);
        make.right.top.offset(0);
        make.bottom.offset(-2);
    }];
    
}
#pragma mark - <UIScrollViewDelegate>
// scrollView滑动的时候调用
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    
    // 获取offset
    CGFloat offsetX = scrollView.contentOffset.x;

    NSLog(@"%f", offsetX);
    UIButton* firstbtn = self.requireBtn;
    UIButton* lastbtn = self.timeDiagnoseBtn;

    // 最大值和最小值的距离
    CGFloat dis = lastbtn.center.x - firstbtn.center.x;

    // 获取这个范围的最小值和最大值
    CGFloat leftValue = -(dis / 2);
    CGFloat rightValue = (dis / 2);
//
    YHValue resValue = YHValueMake(leftValue, rightValue);
    YHValue conValue = YHValueMake(0, 2 * self.scrpView.bounds.size.width);
    CGFloat res = [NSObject resultWithConsult:offsetX andResultValue:resValue andConsultValue:conValue];
    [self.twigView mas_remakeConstraints:^(MASConstraintMaker* make) {
   
        make.width.equalTo(self.requireBtn);
        make.height.offset(2);
        make.bottom.equalTo(self.scrpView);
        make.centerX.offset(res);
    }];
    
    [self.twigView layoutIfNeeded];
}
#pragma mark - <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 每次先从字典中根据IndexPath取出唯一标识符
    NSString *identifier = [_cellDict objectForKey:[NSString stringWithFormat:@"%@", indexPath]];
    // 如果取出的唯一标示符不存在，则初始化唯一标示符，并将其存入字典中，对应唯一标示符注册Cell
    if (identifier == nil) {
        identifier = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@", indexPath]];
        [_cellDict setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];
        // 注册Cell
        [self.dateCollectionView registerClass:[UICollectionViewCell class]  forCellWithReuseIdentifier:identifier];
    }
    // 根据identifier取得对应的cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (indexPath.row == 0) {
        [cell addSubview:self.requirementView];
        // 设置医生接诊条件视图
        [self setRequirement:self.requirementView];
    }else if (indexPath.row == 1)
    {
        UILabel *label = [UILabel labelWithText:@"医生吊炸天，赶紧过来治疗吧!!" andTextColor:[UIColor colorWithHex:0x999999] andFontSize:14];
        [cell addSubview:label];
        // 约束
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(cell);
        }];
        cell.backgroundColor = [UIColor whiteColor];
    }else
    {
        CLTimeViewController *timeCollctionvc = [[CLTimeViewController alloc]init];
        timeCollctionvc.sickDataDict = self.sickDataDict;
        // 添加view
        [cell addSubview:timeCollctionvc.view];
        // 添加父子控制器
        [self addChildViewController:timeCollctionvc];
        // 设置约束
        [timeCollctionvc.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
        cell.backgroundColor = [UIColor orangeColor];
    }
    
    return cell;
}

// 设置医生接诊条件视图
- (void)setRequirement:(UIView *)requirementView
{
    // 设置requirementView的约束
    [requirementView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"caseManagement"]];
    [requirementView addSubview:imageView];
    // 设置imageview的约束
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(60);
        make.center.equalTo(requirementView);
    }];
    // 文字label
    UILabel *textLabel = [UILabel labelWithText:@"暂无接诊条件" andTextColor:[UIColor colorWithHex:0x999999] andFontSize:14];
    // 添加到父视图
    [self.requirementView addSubview:textLabel];
    // 添加约束
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(4);
        make.centerX.equalTo(self.requirementView);
    }];
    
}
// 布置子空间的frame
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    // 不知预约的collectionview的layout
    self.dateLayout.itemSize = self.dateCollectionView.size;
    
}
// 懒加载
- (UIView *)requirementView
{
    if (!_requirementView) {
        _requirementView = [UIView new];
    }
    return _requirementView;
}
// 懒加载字典
- (NSMutableDictionary *)cellDict
{
    if (_cellDict) {
        _cellDict = [NSMutableDictionary dictionary];
    }
    return _cellDict;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 获取星期
- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"Sunday", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}

@end
